package com.project.nimsen.web.pub.tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Component;

import com.project.nimsen.entity.Coltype;
import com.project.nimsen.service.IColtypeService;
import com.project.nimsen.util.WebUtil;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
/**
 * 用于得用户的当前位置
 * */
@Component("nimsen_curpage")
public class CurpageTag extends BaseTag {
	@Resource
	IColtypeService coltypeService;
	/**
	 * @param target <a>标签的打开方式
	 * @param class <a>标签的样式
	 * */
	protected void exec(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		String colid = getAttr("coltypeid");
		String target = getParam("target");
		String clas = getParam("class");
		//没有当前栏目时的处理
		if(StringUtils.isBlank(colid))
			return;
		try {
			//得到当前的栏目信息
			Coltype curcoltype = coltypeService.findEntityById(colid);
			if(curcoltype==null)
				return;
			String rootURL = WebUtil.webUrl(request);
			//首页信息
			StringBuffer outsbf = new StringBuffer("<a href='"+rootURL+"'");
			outsbf = attra(outsbf, target, clas);
			outsbf.append(">首页</a>");
			//String number = curcoltype.getNumber();
			//如果是顶级栏目
			if(StringUtils.isBlank(curcoltype.getPnumber())){
				addcurcol(outsbf, rootURL, curcoltype, target, clas);
				outWrite(outsbf.toString());
				return;
			}
			//siteid
			String siteid = getSessionAttr("pub_siteid");
			List<Map<String, Object>> pcols = coltypeService.
					findAllParentByfPnum(curcoltype.getPnumber(), siteid);
			if(CollectionUtils.isEmpty(pcols)){
				addcurcol(outsbf, rootURL, curcoltype, target, clas);
				outWrite(outsbf.toString());
				return;
			}
			//如果不为空
			for(Map<String, Object> col : pcols){
				String vurl = (String) col.get("vurl");
				outsbf.append("&nbsp;>&nbsp;<a href='");
				outsbf.append(rootURL+"/pub/col").append(vurl);
				outsbf.append("'");
				outsbf = attra(outsbf, target, clas);
				outsbf.append(">").append(col.get("colname")).append("</a>");
			}
			addcurcol(outsbf, rootURL, curcoltype, target, clas);
			outWrite(outsbf.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//设置<a>标签样式
	private StringBuffer attra(StringBuffer sbf, String target, String clas){
		if(StringUtils.isNotBlank(target))
			sbf.append(" target='").append(target).append("'");
		if(StringUtils.isNotBlank(clas))
			sbf.append(" class='").append(clas).append("'");
		return sbf;
	}
	//添加当前栏目
	private StringBuffer addcurcol(StringBuffer outsbf, String rootURL, Coltype curcoltype, String target, String clas){
		outsbf.append("&nbsp;>&nbsp;<a href='");
		outsbf.append(rootURL+"/pub/col").append(curcoltype.getVurl());
		outsbf.append("'");
		outsbf = attra(outsbf, target, clas);
		outsbf.append(">").append(curcoltype.getColname()).append("</a>");
		return outsbf;
	}
}
